try:
    set
except NameError:
    from sets import Set as set # Python 2.3 fallback
import re

class AlreadyRegistered(Exception):
    pass

class NotRegistered(Exception):
    pass

class RegistrationManager(object):
    def __init__(self):
        self.registered_regexes = set()

    def register(self, regex):
        compiled = re.compile(regex)
        if compiled in self.registered_regexes:
            raise AlreadyRegistered("Pattern %s is already registered with simplestats." % regex)
        self.registered_regexes.add(compiled)

    def unregister(self, regex):
        compiled = re.compile(regex)
        if compiled not in self.registered_regexes:
            raise NotRegistered("Pattern %s cannot be removed because it's not registered." % regex)
        self.registered_regexes.remove(compiled)

    def should_log(self, path):
        for regex in self.registered_regexes:
            if path.startswith('/'):
                new_path = path[1:]
            else:
                new_path = path
            if regex.search(new_path):
                return True
        return False

registrar = RegistrationManager()